/**
 * @Date 2023-02-21 14:00:07
 * @Remark
 */

// lib
// config
// script & methods & public
// store
import {
  getCategoryLongPageService,
  getRoomNoPageService,
  getUserBookPageService,
} from "biz/Books/service";
import { state } from "./state";
// service
import { ReqBookPage } from "biz/Books/api";
import { ReqNewBorrow } from "biz/Borrow/api";
import {  addBorrowService, addOrderService } from "biz/Borrow/service";
import toast from "components/toast";

// interface
// 其它

export async function getUserBookPage() {
  const sp = state.getSearch();
  const params: ReqBookPage = {
    pageNum: state.page,
    pageSize: state.size,
    ...sp,
  };
  state.loading = true;
  const result = await getUserBookPageService(params);
  state.loading = false;
  if (result.payload) {
    state.total = result.payload.total;
    state.list = result.payload.list;
  }
}
export async function addBorrowController(params:ReqNewBorrow) {
  state.loading = true;
  const result = await addBorrowService(params);
  state.loading = false;
  if (result.payload) {
    toast.success("借阅成功");
    getUserBookPage()
  }
}
export async function addOrderController(params:ReqNewBorrow) {
  state.loading = true;
  const result = await addOrderService(params);
  state.loading = false;
  if (result.payload) {
    toast.success("预约成功");
    getUserBookPage()
  }
}
// 图书分类不分页列表
export async function getCategoryLongPage() {
  state.loading = true;
  const result = await getCategoryLongPageService({});
  state.loading = false;
  if (result.payload) {
    state.categoryList = result.payload
      .filter((item: CategoryDto) => item.parentCategoryId !== null)
  }
}

export async function getRoomLongPage() {
  // state.loading = true;
  const result = await getRoomNoPageService({});
  // state.loading = false;
  if (result.payload) {
    state.roomList = result.payload;
  }
}

export async function didMount() {
  getCategoryLongPage()
}
